*************
* Figure 8c 8d
*************

set printcolor gs1, permanently
use INTERMEDIATE/firmXgroup_RMS_def10_sampleE_06_15.dta, clear

* Fixed-effects
egen fe_Q=group(module year quarter) // changed from group

* Variables
gen log_size_it=log(rev_ijt)
gen comp2=log(products_ijt)
gen comp1=log_size_it -  comp2

gen sample=0
replace sample=1 if (cohort>=2 & cohort<=23) & age<=16 & age>0 & maxAge>16 & flag_longitudinal=="Complete"
replace age=17 if sample==0 | age>16

qui tab cohort, gen(cohort_d)
foreach num of numlist 4/23 {  // 19
gen cohort_nd`num'=cohort_d`num'-((`num'-1)*cohort_d3-(`num'-2)*cohort_d2)
}


* Groups
bys fe_Q: egen xtile=xtile(log_size_it), nq(10)


** REGRESSION BASELINE

foreach var of varlist log_size_it comp1 comp2 {
preserve
areg  `var' i.xtile cohort_nd* if sample==1, absorb(fe_Q)
matrix results=[e(b)',vecdiag(e(V))']
svmat results
rename results1 coef
rename results2 se

keep coef se
drop if coef == .
gen size=_n
drop if size>10
gen coef_u = coef + 1.96*sqrt(se)
gen coef_l = coef - 1.96*sqrt(se)

save INTERMEDIATE/LifeCycle_firm_`var'_size10.dta, replace
restore
}



// All
preserve
use INTERMEDIATE/LifeCycle_firm_log_size_it_size10.dta, clear
rename coef coef_sales
merge 1:1 size using INTERMEDIATE/LifeCycle_firm_comp1_size10.dta, nogenerate
rename coef coef_comp1
merge 1:1 size using INTERMEDIATE/LifeCycle_firm_comp2_size10.dta, nogenerate
rename coef coef_comp2


twoway ///
(connected  coef_sales size if size<=10, lp(solid) color(midblue) lwidth(vthick)) ///
(connected  coef_comp1 size if size<=10, lp(shortdash) color(navy) lwidth(thick)) ///
(connected  coef_comp2 size if size<=10,  lp(longdash) color(green) lwidth(medthick)) ///
, ytitle("Estimated (log scale)") xtitle("Size (decile)")  ///
title("") xlabel(1(1)10)   ylabel(0(3)12.6)   ///
legend( row(1) order(1 "Firm Sales" 3 "Scope" 2 "Appeal")) ///
graphregion(color(white)) plotregion(fcolor(white)) legend(region(lcolor(white))) note("")
graph export OUTPUT/Figure8c.eps, replace


restore

* FLC

foreach var of varlist log_size_it comp1 comp2 {
preserve

areg `var' i.age_ijt cohort_nd* if sample==1, absorb(fe_Q)
matrix results=[e(b)',vecdiag(e(V))']
svmat results
rename results1 coef
rename results2 se

keep coef se
drop if coef == .
gen age=_n
drop if age>16
gen coef_u = coef + 1.96*sqrt(se)
gen coef_l = coef - 1.96*sqrt(se)

save INTERMEDIATE/LifeCycle_firm_`var'_age.dta, replace
restore
}

// All
use INTERMEDIATE/LifeCycle_firm_log_size_it_age.dta, clear
rename coef coef_sales
merge 1:1 age using INTERMEDIATE/LifeCycle_firm_comp1_age.dta, nogenerate
rename coef coef_comp1
merge 1:1 age using INTERMEDIATE/LifeCycle_firm_comp2_age.dta, nogenerate
rename coef coef_comp2

twoway ///
(connected  coef_sales age if age<=16, lp(solid) color(midblue) lwidth(thick)) ///
(connected  coef_comp1 age if age<=16, lp(shortdash) color(navy) lwidth(thick)) ///
(connected  coef_comp2 age if age<=16,  lp(longdash) color(green) lwidth(medthick)) ///
, ytitle("Estimated (log scale)") xtitle("Age (quarters)")  ///
title("") ylabel(0(0.25)1.02) xlabel(1 4 8 12 16) ///
legend( row(1) order(1 "Firm Sales" 3 "Scope" 2 "Appeal")) ///
graphregion(color(white)) plotregion(fcolor(white)) legend(region(lcolor(white))) note("")
graph export OUTPUT/Figure8d.eps, replace


